package com.stylefeng.guns.modular.vpn_order_info.service.impl;

import com.stylefeng.guns.core.common.constant.DatasourceEnum;
import com.stylefeng.guns.core.mutidatasource.annotion.DataSource;
import com.stylefeng.guns.modular.system.dao.VpnFilterMapper;
import com.stylefeng.guns.modular.system.model.VpnOrderInfo;
import com.stylefeng.guns.modular.system.dao.VpnOrderInfoMapper;
import com.stylefeng.guns.modular.vpn.dto.VpnOrderSearchDTO;
import com.stylefeng.guns.modular.vpn.utils.PoiExcelUtil;
import com.stylefeng.guns.modular.vpn.vo.OrderFilterInfoVO;
import com.stylefeng.guns.modular.vpn.vo.Paging;
import com.stylefeng.guns.modular.vpn_order_info.service.IVpnOrderInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  订单类服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2020-03-02
 */
@Service
@Slf4j
public class VpnOrderInfoServiceImpl extends ServiceImpl<VpnOrderInfoMapper, VpnOrderInfo> implements IVpnOrderInfoService {

    @Autowired
    private VpnOrderInfoMapper orderInfoMapper;

    @Autowired
    private VpnFilterMapper vpnFilterMapper;

    @Override
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public Paging findAllOrderInfo(VpnOrderSearchDTO searchDTO) {
        int start = searchDTO.getPage() > 0 ? searchDTO.getPage() - 1 : 0;
        int count = searchDTO.getLimit();
        searchDTO.setStart(start * count);
        searchDTO.setCount(count);

        Paging<Map<String, Object>> paging = new Paging<>();

        Long total = orderInfoMapper.queryTotalCount(searchDTO);
        List<Map<String, Object>> items = orderInfoMapper.findOrderPage(searchDTO);

        paging.setTotal(total);
        paging.setItems(items);
        return paging;
    }

    @Override
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public OrderFilterInfoVO getFilterInfo() {
        OrderFilterInfoVO filterInfoVO = new OrderFilterInfoVO();
        filterInfoVO.setPackageFilters(vpnFilterMapper.queryPackageList());
        filterInfoVO.setPaymentTypes(vpnFilterMapper.queryPaymentTypeList());
        return filterInfoVO;
    }

    /**
     * 导出订单数据
     */
    @Override
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public void exportOrderInfo(String userName, String packageId,
                                String paymentType, String orderStatus,
                                String beginTime,String endTime,
                                HttpServletResponse response) {
        VpnOrderSearchDTO searchDTO = new VpnOrderSearchDTO();
        searchDTO.setPackageId(packageId);
        searchDTO.setPaymentType(paymentType);
        searchDTO.setUserName(userName);
        searchDTO.setOrderStatus(orderStatus);
        searchDTO.setBeginTime(beginTime);
        searchDTO.setEndTime(endTime);

        List<Map<String,Object>> orderInfo =
                orderInfoMapper.findAllOrderInfo(searchDTO);

        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建一个Excel表单，参数为sheet的名字
        HSSFSheet sheet = workbook.createSheet("订单数据");
        List<String> colList = new ArrayList<>();
        colList.add("付款时间");
        colList.add("用户名");
        colList.add("套餐分类");
        colList.add("套餐名称");
        colList.add("支付方式");
        colList.add("订单状态");
        colList.add("套餐价格(HKD)");
        colList.add("已收金额(HKD)");
        //创建表头
        PoiExcelUtil.setTableHeader(workbook,sheet,colList);

        //新增数据行，并且设置单元格数据
        int rowNum = 1;
        for (Map<String,Object> order: orderInfo){
            HSSFRow row = sheet.createRow(rowNum);
            row.createCell(0).setCellValue(""+order.get("createTime"));
            row.createCell(1).setCellValue(""+order.get("userName"));
            row.createCell(2).setCellValue(order.get("packageType").equals("1")?"固定":"活动");
            row.createCell(3).setCellValue(""+order.get("packageName"));
            if(order.get("paymentWay").equals("1")){
                row.createCell(4).setCellValue("支付宝");
            }
            else if(order.get("paymentWay").equals("2")){
                row.createCell(4).setCellValue("微信支付");
            }
            else if(order.get("paymentWay").equals("3")){
                row.createCell(4).setCellValue("银联");
            }
            else if(order.get("paymentWay").equals("4")){
                row.createCell(4).setCellValue("VISA卡");
            }
            else if(order.get("paymentWay").equals("5")){
                row.createCell(4).setCellValue("Master卡");
            }
            else if(order.get("paymentWay").equals("6")){
                row.createCell(4).setCellValue("IOS内部支付");
            }
            else if(order.get("paymentWay").equals("7")){
                row.createCell(4).setCellValue("Google支付");
            }
            else if(order.get("paymentWay").equals("8")){
                row.createCell(4).setCellValue("手动添加");
            }

            if(0 == (Integer) order.get("status")){
                row.createCell(5).setCellValue("待支付");
            }
            else if(1 == (Integer) order.get("status")){
                row.createCell(5).setCellValue("支付成功");
            }
            else if(2 == (Integer) order.get("status")){
                row.createCell(5).setCellValue("支付失败");
            }
            row.createCell(6).setCellValue(""+order.get("money"));
            if (1 == (Integer) order.get("status")){
                row.createCell(7).setCellValue(""+order.get("money"));
            }else{
                row.createCell(7).setCellValue("0");
            }
            rowNum++;
        }
        String fileName = "order-info-"+System.currentTimeMillis()+".xls";
        //写入文件并将文件已二进制流的方式返回为调用者
        PoiExcelUtil.writeWorkBook(response,workbook,fileName);
    }

}
